
glob_answers = []
glob_question = ''
glob_tmp_ask_question = ''
glob_tmp_ask_question_uuid = ''
glob_deleteCard_clock_min = 0

function intialize(){
	var xhr = getAjaxObj()
	cmdStr = 'room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/getAllPalyers',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			drawAllplayers(result)
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 
}

function skipQuestion(){

	var xhr = getAjaxObj()
	cmdStr = 'room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&player_uuid=' + parseArgs('player_uuid')
	cmdStr += '&question_uuid=' + glob_tmp_ask_question_uuid
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/skipQuestion',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			glob_answers = []
			glob_question = ''
			glob_tmp_ask_question = ''
			glob_tmp_ask_question_uuid = ''

			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'giveQuestion'){
				glob_tmp_ask_question = SafeStringToSpecial(result['question'])
				glob_tmp_ask_question_uuid = result['uuid']

				drawChooseQuestion(result['question'])
			} else if(result['result'] == 'giveAnswer'){
				try{ glob_answers = JSON.parse(result['answers']) }
				catch(err){ glob_answers = result['answers'] }

				drawAnswers('')

			}else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 

}

function waitForResponse(selectedAnswer){

	html = '<div id=nextRoundID>Click on next round when everybody is ready </div><br><br>' 
	html += drawQuestion(glob_question) 
	html += drawAnswer(selectedAnswer, '', '') 
	html += getOptions('nextRound', 'nextRound', '220', '60')
	document.getElementById("contentID").innerHTML = html

}

function nextRound(){
	glob_answers = []

	var xhr = getAjaxObj()
	cmdStr = 'room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&player_uuid=' + parseArgs('player_uuid')
	cmdStr += '&question=' + specialToSafe(glob_question)
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/nextRound',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			glob_question = ''
			glob_tmp_ask_question = ''
			glob_tmp_ask_question_uuid = ''

			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'giveQuestion'){
				glob_tmp_ask_question = SafeStringToSpecial(result['question'])
				glob_tmp_ask_question_uuid = result['uuid']

				drawChooseQuestion(result['question'])
			} else if(result['result'] == 'giveAnswer'){
				try{ glob_answers = JSON.parse(result['answers']) }
				catch(err){ glob_answers = result['answers'] }

				drawAnswers('')

			}else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 
}

function answerClicked(selectedAnswer, answer_uuid){
	
	if(document.getElementById("nextRoundID") != null) return

	yes = confirm('Are you sure this is the answer you want?')
	if(yes == false) return

	var xhr = getAjaxObj()

	cmdStr = 'user_uuid=' + parseArgs('player_uuid')
	cmdStr += '&room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&selectedAnswer=' + selectedAnswer
	cmdStr += '&answer_uuid=' + answer_uuid
	cmdStr += '&fuckIE=' + randomStr(6)

	selectedAnswer = SafeStringToSpecial(selectedAnswer)

	if(document.getElementById("removeCardDivID") != null){
		submitFunc = '/removeAnswer' 
	}
	else if(glob_tmp_ask_question != ''){ 
		submitFunc = '/submitAnswerChosen' 
	}
	else{ 
		submitFunc = '/submitAnswer' 
		waitForResponse(selectedAnswer)
	}

	xhr.open('POST', submitFunc,  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'ok'){
				
			}
			else if(result['result'] == 'cardRemoved'){
				showCards()

				var dTime = new Date();
				var hours = dTime.getHours();
				var minute = dTime.getMinutes();
			
				 glob_deleteCard_clock_min = minute + hours*60

				alert('The card has been remove and will be replaced by another one')
			}
			else if(result['result'] == 'userWon'){
				showCards()
				alert(result['msg'])
			} else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 


}

function skipPerson(user_uuid){

	var xhr = getAjaxObj()
	cmdStr = 'user_uuid=' + user_uuid
	cmdStr += '&room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/deleteUser',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'ok'){
				setTimeout(showCards, 3000)
				alert(result['msg'])
			}
			else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 


}

function showQuestion(){

	var xhr = getAjaxObj()
	cmdStr = 'room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&user_uuid=' + parseArgs('player_uuid')
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/getRoomQuestion',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'ok'){
				glob_question = result['question']
				if(glob_question == ''){ alert('The question has not been selected yet'); return; }
				drawAnswers('')
			}
			else if(result['result'] == 'is questioner'){
				showCards()
			}
			else if(result['result'] == 'not yet'){
				msg = result['msg']['user_name'] + ' has not yet picked a question, press "cancel" delete this person? '
				msg += ' (note : 2 cancel votes are required to delete the person)'
				ans = confirm(msg)
				if(ans == false){
					ans = confirm("Are you sure you want to kick " + result['msg']['user_name'] + " out of the room.")
					if(ans == true){
						skipPerson(result['msg']['uuid'])
					}
				}
			}
			else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 

}


function drawAnswers(selectedAnswer){

	html = getOptions('intialize', 'playerList', '140', '45') + ' '
	html += getOptions('showCards', 'myCards', '140', '45') + '<br><br>'
 	if(glob_question == ''){
		html += getOptions('showQuestion', 'showQuestion', '230', '50') 
	}else{
		html += drawQuestion(glob_question) + '<br><br>'

	}

	html += '<br><br>'
	for(i in glob_answers){
		if(glob_answers[i] != ''){
			html += drawAnswer(glob_answers[i], i, selectedAnswer) + '<br>'
		}
	}
	html += getOptions('replaceCard', 'replaceCard', 240, 60) + '<br>'
	document.getElementById("contentID").innerHTML = html
}

function replaceCard(){
	var dTime = new Date();
	var hours = dTime.getHours();
	var minute = dTime.getMinutes();

	diffT = minute + hours*60 - glob_deleteCard_clock_min
	if( diffT < 5 && diffT > 0){
		alert('You can only replace a card every 5 minutes')
		return
	}

	html = '<div id=removeCardDivID style="text-align:left;"><a href="javascript:showCards();">'
	html += '<img src="img/goBack.png" alt="" width="140px" height="45px" style="margin-bottom:8px;" /></a></div>'
	html += '<br><br>'
	html += 'Pick the card you want to remove<br><br>'
	for(i in glob_answers){
		if(glob_answers[i] != ''){
			html += drawAnswer(glob_answers[i], i, '') + '<br>'
		}
	}
	document.getElementById("contentID").innerHTML = html
}


function drawCzarOptions(answers){
	answers = answers.split('|next-Answer|')
	html = 'Please read the question and the answers for everybody and then click on the best card<br><br>'
	html += drawQuestion(glob_tmp_ask_question)
	for(i in answers){
		if(answers[i] != ''){
			html += drawAnswer(answers[i], '', '')
		}
	}
	document.getElementById("contentID").innerHTML = html
}

function viewAnswers(){
	var xhr = getAjaxObj()
	cmdStr = 'room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/getEverybodysAnswer',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'ok'){
				drawCzarOptions(result['answers'])
			} else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 

}

function drawViewAnswer(){
	html = 'Press View Answers when everybody is ready<br><br>' 
	html += drawQuestion(glob_tmp_ask_question)
	html += '<br><br><br><br>'
	html += getOptions('viewAnswers', 'viewAnswers', 240, 50)
	document.getElementById("contentID").innerHTML = html
}

function useQuestion(){

	var xhr = getAjaxObj()
	cmdStr = 'room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&user_uuid=' + parseArgs('player_uuid')
	cmdStr += '&questionChosen=' + specialToSafe(glob_tmp_ask_question)
	cmdStr += '&question_uuid=' + glob_tmp_ask_question_uuid
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/onSelectQuestion',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'ok'){
				drawViewAnswer()
			} else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 
}

function drawChooseQuestion(question){

	html = 'Decide on a question. <br><br>' 
	html += getOptions('skipQuestion', 'skipQuestion', '150', '45') + ' '
	html += getOptions('useQuestion', 'useQuestion', '150', '45') + '<br><br>'
	html += '<div style="width:90%;">'
	html += SafeStringToSpecial(question)
	html += '</div>'
	document.getElementById("contentID").innerHTML = html

}

function showCards(){
	glob_answers = []
	glob_question = ''
	glob_tmp_ask_question = ''
	glob_tmp_ask_question_uuid = ''

	var xhr = getAjaxObj()
	cmdStr = 'room_uuid=' + parseArgs('room_uuid')
	cmdStr += '&player_uuid=' + parseArgs('player_uuid')
	cmdStr += '&fuckIE=' + randomStr(6)

	xhr.open('POST', '/getPlayerCards',  true); 
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			result = JSON.parse(xhr.responseText)
			if(result['result'] == 'giveQuestion'){
				glob_tmp_ask_question = SafeStringToSpecial(result['question'])
				glob_tmp_ask_question_uuid = result['uuid']

				drawChooseQuestion(result['question'])
			} else if(result['result'] == 'giveAnswer'){
				try{ glob_answers = JSON.parse(result['answers']) }
				catch(err){ glob_answers = result['answers'] }

				drawAnswers('')

			}else{
				alert(result['error'])
			}
		}
		else if(AjaxCheck(xhr) == 0) {
			
		}
	}; 

	xhr.send(cmdStr); 
}

function drawAllplayers(playerList){
	html = getOptions('intialize', 'playerList', '140', '45') + ' '
	html += getOptions('showCards', 'myCards', '140', '45') + '<br>'

	html += createUser('player name', 'points')
	html += '<br><hr>'
	for(i in playerList){
		html += createUser(playerList[i]['user_name'], playerList[i]['points']) + '<br>'
	}
	html += '<br><hr style="width:90%;"><br><br>'
	html += getOptions('intialize', 'refreshForMore', '240', '65') + '<br>'
 
	document.getElementById("contentID").innerHTML = html
}
